$(document).ready(function(){
	$('#add').html('<a href="add_staff.html"><img src="../images/anniu16.gif"/></a>');
	getDepartmentList();
	cw.common.getUserSession(getStaffList,true);
})

//验证权限
function validatePermission(userinfo){
	var permissionNames = "add,del,lock,update,getInfo,empowersb,resetpwd";
	var actionNames = "ADD_T_SYS_USER_ACTION,DELETE_T_SYS_USER_ACTION,UPDATE_SYS_USER_ISLOCK_ACTION,UPDATE_T_SYS_USER_ACTION,";
	actionNames += "GET_T_SYS_USER_ACTION,MANAGE_USER_PERMISSION_ACTION,RESET_SYS_USER_PASSWORD_ACTION";
	cw.common.hasPermission(permissionNames,actionNames,isShow);
	getUserList();
}
//是否显示操作按钮
function isShow(data){
	var obj = data.root.PERMISSION.rs[0];
	if(obj.add){
		$('#add').html('<a href="add_staff.html"><img src="../images/anniu16.gif"/></a>');
	}
	del = obj.del;
	if(del){
		$('#batchDel').html('<a href="javascript:batchDelUser()">批量删除</a>');
	}
	update = obj.update;
	lock = obj.lock;
	getInfo = obj.getInfo;
	empowersb = obj.empowersb;
	resetpwd = obj.resetpwd;
}

function getDepartmentList(){
	
	var node = $('#DEPARTMENT_NO');
	
	$.post(cw.common.path + '/json?action=QUERY_T_DEPARTMENT_ACTION',{},function(data){
		if(data.success){
			$.each(eval(data.root.T_DEPARTMENT.rs),function(i,n){
				node.append('<option value="'+this.DEPARTMENT_NO+'">'+this.DEPARTMENT_NAME+'</option>');
			})
		} else {
			alert(data.errors.errmsg);
		}
	},'json');
}

function getStaffList(){
	var url = cw.common.path + '/json?action=QUERY_T_PERSONNALBY_PAGER_ACTION';
	url += "&NAME="+$('#NAME').val()+"&DEPARTMENT_NO="+$('#DEPARTMENT_NO').val();
	var xmlname = "T_PERSONNAL";
	var pagePanel='#pager';
	var result='#list';
	var pageSize=10;
	var defaultHtml='<li><em style="text-align: center;width:100%">没有搜索到您要查询的数据...</em></li>';
	cw.common.changePage(url,pageSize,xmlname,result,fn_getStaffList,pagePanel,defaultHtml);
}

function fn_getStaffList(obj,result,i){
	var str = "<li>";
	str += "<em style='width: 5%; text-align: center;'><input type='checkbox' name='check' value='"+obj.PERSONNAL_NO+"' /></em>";
	str += "<em style='width: 15%; text-align: center;''>"+obj.NAME+"</em>";
	str += "<em style='width: 15%; text-align: center;'>"+obj.SEX+"</em>";
	str += "<em style='width: 15%; text-align: center;''>"+obj.DEPARTMENT_NO+"</em>";
	str += "<em style='width: 15%; text-align: center;''>"+obj.POSITION+"</em>";
	str += "<em style='width: 15%; text-align: center;''>"+obj.TELEPHONE+"</em>";
	str += "<em style='width: 14%; text-align: center;'>";
//	if(update){
		str += "<a href='update_staff.html?id="+obj.PERSONNAL_NO+"'>修改</a> ";
//	}
//	if(del){
		str += "<a href='javascript:delStaff("+obj.PERSONNAL_NO+",\""+obj.NAME+"\")'>删除</a> ";
//	}
//	if(!getInfo && !update && !del){
//		str += "您无操作权限";
//	}
	str += "</em></li>";
	$(result).append(str);
}

//删除用户
function delStaff(id,name){
	if(confirm("是否确定删除"+name+"的人员信息？")){
		var url = cw.common.path + '/json?action=DELETE_T_PERSONNAL_ACTION&PERSONNAL_NO='+id;
		$.post(url,{},function(data){
			if(data.success){
				alert('已成功删除人员'+name+'！');
				location.href = document.URL;
			} else {
				alert(data.errors.errmsg);
			}
		},'json');
	}
}

//批量删除
function batchDelStaff(){
	if(getCheckedNum('check')==0){
		alert('请选择要删除的人员');
		return;
	}
	if(!confirm('确定要删除人员吗？')){
		return;
	}
	$.post(cw.common.path + '/json',
		{
			action : 'BATCH_DELETE_T_PERSONNAL_ACTION',
			PERSONNAL_NO : getCheckedVal('check').split(',')
		}, function(data){
			if (data.success) {
				alert('批量删除人员成功');
				location.href = document.URL;
			} else {
				alert(data.errors.errmsg);
			}
		}, 'json');
}

